package com.example.inter.tourdemo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.inter.tourdemo.R;
import com.example.inter.tourdemo.adapter.homeadapter.HomechannelAdapter;
import com.example.inter.tourdemo.bean.homebean.HomeChannelItem;
import com.example.inter.tourdemo.ui.GlideImageLoader;
import com.example.inter.tourdemo.utils.MyApplication;
import com.sunfusheng.marqueeview.MarqueeView;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by Inter巩海亮 on 2017-03-28.
 * 微信:
 * QQ号:
 * 邮箱:dahaizhiliang@126.com
 */

public class HomeFragment extends SupportFragment {

//    01_轮播图数据
    private List bannerlist;

    private RecyclerView home_channel_rv;

    //设置频道item的集合
    private ArrayList<HomeChannelItem> mDataList;

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private View view;
    //跑马灯初始化
    private MarqueeView marqueeView;
    private Banner banner;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        //设置数据
        initData();
        //设置视图
        initView();
        initAdapter();
        //设置滑动加速
        LinearLayoutManager layoutManager = new LinearLayoutManager(MyApplication.getContext());
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        return view;
    }

    private void initView() {
        //01_轮播图实现
        banner = (Banner) view.findViewById(R.id.banner);
        banner.setImages(bannerlist)
                .setImageLoader(new GlideImageLoader())
                .start();

        //02_频道视图
        home_channel_rv = (RecyclerView) view.findViewById(R.id.home_channel_rv);
        StaggeredGridLayoutManager layoutGrid = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.HORIZONTAL);
        layoutGrid.setAutoMeasureEnabled(true);
        home_channel_rv.setHasFixedSize(true);
        home_channel_rv.setNestedScrollingEnabled(false);
        home_channel_rv.setLayoutManager(layoutGrid);

        //跑马灯实现
        marqueeView = (MarqueeView) view.findViewById(R.id.marqueeView3);
        marqueeView.startWithText("我为旅游代言,我就是我,漫兔的杰作!每个产品都是我们精心设计.");
    }



    //初始数据
    private void initData() {
        //    01_轮播图数据
        bannerlist = new ArrayList();
        Integer[] bannerimages = {R.drawable.banner01, R.drawable.banner02, R.drawable.banner03};
        for (int i = 0; i < bannerimages.length; i++) {
            bannerlist.add(bannerimages[i]);
        }

        //频道数据
        String[] TITLE = {"主题游", "个性定制", "旅游尾单", "跟团游", "漫客传奇",
                "当地特产", "旅行必备", "特色礼品", "活动专区", "小曼推荐"};

        int[] IMG = {R.drawable.ic_category_0, R.drawable.ic_category_1, R.drawable.ic_category_2,
                R.drawable.ic_category_3, R.drawable.ic_category_4, R.drawable.ic_category_5,
                R.drawable.ic_category_6, R.drawable.ic_category_7, R.drawable.ic_category_0,
                R.drawable.ic_category_0};

        mDataList = new ArrayList<>();

        for (int i = 0; i < TITLE.length; i++) {
            HomeChannelItem item = new HomeChannelItem();
            item.setTitle(TITLE[i]);
            item.setImageResource(IMG[i]);
            mDataList.add(item);
        }
        //精品推荐数据

    }

    private void initAdapter() {
        BaseQuickAdapter homechannelAdapter = new HomechannelAdapter(R.layout.home_channel_item_view, mDataList);
//        homeAdapter.openLoadAnimation();


        //设置点击事件
//        homechannelAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Toast.makeText(getContext(), "我被点击了"+position, Toast.LENGTH_SHORT).show();
//            }
//        });



        home_channel_rv.setAdapter(homechannelAdapter);
    }





    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


}


